import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Main from '../views/Main.vue'
import Banner from '../views/Banner.vue'
import Myspec from '../views/Myspec.vue'
import Detail from '../views/Detail.vue'
import Info from '../views/Info.vue'
import Dinfo from '../views/Dinfo.vue'
import Score from '../views/Score.vue'
import Order from '../views/Order.vue'
import Safe from "../views/Safe.vue"

import GoodCategory from "../views/GoodCategory.vue"
import GoodList from "../views/GoodList.vue"
import Register from "../views/Register"
import Login from "../views/Login"
import Cart from '../views/Cart.vue'
import PerCenter from '../views/PerCenter.vue'
import AddressList from '../views/AddressList.vue'
import AddressEdit from '../views/AddressEdit.vue'
import CheckOut from '../views/CheckOut.vue'
import UpdatePwd from "../views/UpdatePwd.vue"

import store from '../store'
import AllOrder from '../views/AllOrder.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Main',
    component: Main,
    children: [{
        path: '/',
        name: 'Home',
        component: Home
      },
      {
        path: '/banner',
        component: Banner
      },
      {
        path: '/myspec',
        component: Myspec
      },
      {
        path: "/list",
        name: "GoodCategory",
        component: GoodCategory
      },
      {
        path: "/goodlist",
        name: "GoodList",
        component: GoodList
      },
      {
        path:"/order",
        name:"Order",
        component:Order
      },
      {
        path:"/safe",
        name:"Safe",
        component:Safe
      }
    ]
  },
  {
    path: '/detail/:browse',
    component: Detail,
    children: [{
        path: '/detail/:browse',
        component: Info,
      },
      {
        path: '/info',
        component: Info
      },
      {
        path: '/dinfo',
        component: Dinfo
      },
      {
        path: '/score',
        component: Score
      },
    ]
  },
  {
    path: "/register",
    name: "Register",
    component: Register
  },
  {
    path: "/login",
    name: "Login",
    component: Login
  }, 
  {
    path: '/cart',
    name: 'Cart',
    component: Cart,
  },
  {
    path: '/percenter',
    name: 'PerCenter',
    component: PerCenter,
  },
  {
    path: '/addresslist',
    name: 'AddressList',
    component: AddressList,
  },
  {
    path: '/addressedit',
    name: 'AddressEdit',
    component: AddressEdit,
  },
  {
    path:'/checkout',
    name:'CheckOut',
    component:CheckOut,
  },
  {
    path:'/updatepwd',
    name:'UpdatePwd',
    component:UpdatePwd,
    path:'/allorder',
    name:'AllOrder',
    component:AllOrder,
  },
]


const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  let {path} = to //用户期望访问的路由
  if(path.indexOf('/cart')!=-1){  //判断路由是否为购物车
    let {userInfo} = store.state.user
    if(userInfo){
      next()
    }else{
      next('/login')
    }
  }
  next() //其他路由直接放行
})

export default router